<?php

namespace app\merchant\controller\apps\sharp;

use app\merchant\controller\Base;
use app\merchant\model\Goods as GoodsModel;
use app\merchant\service\Goods as GoodsService;
use app\merchant\model\sharp\Goods as SharpGoodsModel;

/**
 * 秒杀商品管理
 * Class Goods
 * @package app\merchant\controller\apps\sharp
 */
class Goods extends Base
{

    /**
     * 秒杀商品列表
     * @param string $search
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        $model = new SharpGoodsModel;
        $list = $model->getList($param);

        return $this->renderSuccess($list,"查询成功");
    }

    /**
     * 编辑秒杀商品
     * @param $sharp_goods_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function detail()
    {
        $param = $this->postData();
        if(!isset($param['sharp_goods_id']) || isZeroNumeric($param['sharp_goods_id'])){
            return $this->renderError([],"参数错误");
        }
        // 秒杀商品详情
        $model = SharpGoodsModel::detail($param['sharp_goods_id'], ['sku']);

        if(!$model){
            return $this->renderError([],"秒杀商品不存在，请刷新重试");
        }
        // 商品信息
        $goods = GoodsModel::detail($model['goods_id']);
        // 商品多规格信息
        $specData = $model->getSpecData($goods, $model['sku']);

        return $this->renderSuccess(compact('model', 'goods', 'specData'),"查询成功");

    }



    /**
     * 添加秒杀商品：步骤1
     * @return mixed
     */
    public function step()
    {
        $param = $this->postData();
        if(!isset($param['goods_id']) || isZeroNumeric($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = new SharpGoodsModel;
        // 验证商品ID能否被添加
        if (!$model->validateGoodsId($param['goods_id'])) {
            return $this->renderError([],$model->getError());
        }
        // 商品信息
        $goods = GoodsModel::detail($param['goods_id']);

        $specData = GoodsService::getSpecData($goods);

        return $this->renderSuccess(compact('goods', 'specData'),"查询成功");
    }


    /**
     * 添加秒杀商品：步骤2
     * @param $goodsId
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {

        $param = $this->postData();
        if(!isset($param['goods_id']) || isZeroNumeric($param['goods_id'])){
            return $this->renderError([],"参数错误");
        }
        $model = new SharpGoodsModel;
        // 商品信息
        $goods = GoodsModel::detail($param['goods_id']);

        // 表单提交
        if ($model->add($goods, $this->postData())) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError([],$model->getError() ?: '添加失败');
    }

    /**
     * 编辑秒杀商品
     * @param $sharp_goods_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function edit()
    {
        $param = $this->postData();

        if(!isset($param['sharp_goods_id']) || isZeroNumeric($param['sharp_goods_id'])){
            return $this->renderError([],"参数错误");
        }
        // 秒杀商品详情
        $model = SharpGoodsModel::detail($param['sharp_goods_id']);

        if(!$model){
            return $this->renderError([],"秒杀商品不存在，请刷新重试");
        }
        // 商品信息
        $goods = GoodsModel::detail($model['goods_id']);

        // 更新记录
        if ($model->edit($goods, $param)) {
            return $this->renderSuccess([],'编辑成功');
        }
        return $this->renderError([],$model->getError() ?: '编辑失败');
    }

    /**
     * 删除秒杀商品
     * @param $sharp_goods_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();

        if(!isset($param['sharp_goods_id']) || isZeroNumeric($param['sharp_goods_id'])){
            return $this->renderError([],"参数错误");
        }

        // 秒杀商品详情
        $model = SharpGoodsModel::detail($param['sharp_goods_id']);
        if(!$model){
            return $this->renderError([],"秒杀商品不存在，请刷新重试");
        }

        if (!$model->setDelete()) {
            return $this->renderError([],$model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}